package com.qianyu.project.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianyu.project.core.context.user.FlowUserManager;
import com.qianyu.project.entity.SystemUser;
import com.qianyu.project.entity.TenderBiddingHistory;
import com.qianyu.project.entity.TenderPurchasePlan;
import com.qianyu.project.mapper.TenderBiddingHistoryMapper;
import com.qianyu.project.service.ISystemUserService;
import com.qianyu.project.service.ITenderBiddingHistroyService;
import com.qianyu.project.service.ITenderCompanyService;
import com.qianyu.project.service.ITenderPurchasePlanService;
import com.qianyu.project.utils.AssertTender;
import com.qianyu.project.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author gs
 * @date create in 2021/2/3 10:29
 */
@Service
public class TenderBiddingHistroyServiceImpl extends ServiceImpl<TenderBiddingHistoryMapper, TenderBiddingHistory> implements ITenderBiddingHistroyService {

    @Autowired
    private TenderBiddingHistoryMapper tenderBiddingHistoryMapper;
    @Autowired
    private FlowUserManager flowUserManager;
    @Autowired
    private ISystemUserService systemUserService;
    @Autowired
    private ITenderCompanyService tenderCompanyService;
    @Autowired
    private ITenderPurchasePlanService tenderPurchasePlanService;

    @Override
    public void submit(TenderBiddingHistory tenderBiddingHistory) {
        //查询公司注册资本
        Integer companyId = AssertTender.isValidate(systemUserService, flowUserManager, tenderCompanyService, tenderBiddingHistory.getOffer().doubleValue());
        //设置默认值
        tenderBiddingHistory.setCreatedate(DateUtil.now());
        tenderBiddingHistory.setCompanyId(companyId);
        tenderBiddingHistoryMapper.insert(tenderBiddingHistory);
    }

    @Override
    public TenderBiddingHistory isExist(String tenderId) {
        SystemUser systemUser = systemUserService.getById(flowUserManager.getFlowUser().getId());
        return tenderBiddingHistoryMapper.selectOne(
                new QueryWrapper<TenderBiddingHistory>()
                        .eq("company_id", systemUser.getCompanyId())
                        .eq("tender_id", tenderId)
                        .ne("status", "5")
        );
    }

    @Override
    public void update(TenderBiddingHistory tenderBiddingHistory) {
        //查询公司注册资本
        AssertTender.isValidate(systemUserService, flowUserManager, tenderCompanyService, tenderBiddingHistory.getOffer().doubleValue());
        tenderBiddingHistory.setCreatedate(DateUtil.now());
        tenderBiddingHistoryMapper.updateById(tenderBiddingHistory);
    }

    @Override
    public IPage<TenderBiddingHistory> getPageList(Map<String, String> params) {
        SystemUser systemUser = systemUserService.getById(flowUserManager.getFlowUser().getId());
        //验证公司是否为空
        if(systemUser.getCompanyId() != null){
            params.put("companyId", systemUser.getCompanyId().toString());
        }
        return tenderBiddingHistoryMapper.selectPageList(PageUtil.createPage(params),params);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void cooperation(TenderBiddingHistory tenderBiddingHistory) {
        //修改当前记录为中标
        tenderBiddingHistoryMapper.updateById(tenderBiddingHistory);
        //修改其他竞争对手为未中标
        TenderBiddingHistory other = new TenderBiddingHistory();
        other.setStatus("4");
        tenderBiddingHistoryMapper.update(
                other,
                new UpdateWrapper<TenderBiddingHistory>()
                .eq("tender_id", tenderBiddingHistory.getTenderId())
                .ne("id", tenderBiddingHistory.getId())
        );
        //修改招标对象状态为中标
        TenderPurchasePlan tenderPurchasePlan = new TenderPurchasePlan();
        tenderPurchasePlan.setId(tenderBiddingHistory.getTenderId());
        tenderPurchasePlan.setStatus("3");
        tenderPurchasePlanService.updateById(tenderPurchasePlan);
    }
}
